package com.zhwl.manage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhwl.manage.domain.RateTreeCategories;
import com.zhwl.manage.mapper.RateTreeCategoriesMapper;
import com.zhwl.manage.service.IRateTreeCategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 增值税分类Service业务层处理
 *
 * @author mobin
 * @date 2025-07-22
 */
@Service
public class RateTreeCategoriesServiceImpl extends ServiceImpl<RateTreeCategoriesMapper, RateTreeCategories> implements IRateTreeCategoriesService {
    @Autowired
    private RateTreeCategoriesMapper rateTreeCategoriesMapper;

    /**
     * 查询增值税分类
     *
     * @param id 增值税分类主键
     * @return 增值税分类
     */
    @Override
    public RateTreeCategories selectRateTreeCategoriesById(Long id) {
        return rateTreeCategoriesMapper.selectRateTreeCategoriesById(id);
    }

    /**
     * 查询增值税分类列表
     *
     * @param rateTreeCategories 增值税分类
     * @return 增值税分类
     */
    @Override
    public List<RateTreeCategories> selectRateTreeCategoriesList(RateTreeCategories rateTreeCategories) {
        return rateTreeCategoriesMapper.selectRateTreeCategoriesList(rateTreeCategories);
    }

    /**
     * 新增增值税分类
     *
     * @param rateTreeCategories 增值税分类
     * @return 结果
     */
    @Override
    public int insertRateTreeCategories(RateTreeCategories rateTreeCategories) {
        return rateTreeCategoriesMapper.insertRateTreeCategories(rateTreeCategories);
    }

    /**
     * 修改增值税分类
     *
     * @param rateTreeCategories 增值税分类
     * @return 结果
     */
    @Override
    public int updateRateTreeCategories(RateTreeCategories rateTreeCategories) {
        return rateTreeCategoriesMapper.updateRateTreeCategories(rateTreeCategories);
    }

    /**
     * 批量删除增值税分类
     *
     * @param ids 需要删除的增值税分类主键
     * @return 结果
     */
    @Override
    public int deleteRateTreeCategoriesByIds(Long[] ids) {
        List<RateTreeCategories> rateTreeCategoriesList = rateTreeCategoriesMapper.selectList(null);
        for(Long id: ids){
            rateTreeCategoriesList.forEach(item -> {
                if(Objects.equals(item.getParentId(), id)){
                    throw new RuntimeException("请先删除子级分类");
                }
            });
        }
        return rateTreeCategoriesMapper.deleteRateTreeCategoriesByIds(ids);
    }

    /**
     * 删除增值税分类信息
     *
     * @param id 增值税分类主键
     * @return 结果
     */
    @Override
    public int deleteRateTreeCategoriesById(Long id) {
        return rateTreeCategoriesMapper.deleteRateTreeCategoriesById(id);
    }
}
